/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.entity.MgOpenAdvertisementEntity;
import com.migo.entity.OpeningAdvertisementEntity;
import com.migo.entity.SmsCountryEntity;
import com.migo.service.MgOpenAdvertisementService;
import com.migo.service.OpeningAdvertisementService;
import com.migo.service.TokenService;
import com.migo.utils.JsonUtils;
import com.migo.utils.R;
import com.migo.utils.redis.RedisCacheUtil;
import com.migo.validator.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 开屏广告控制类
 */
@RestController
@RequestMapping("/api")
public class ApiOpeningImageController {

    @Autowired
    private MgOpenAdvertisementService mgOpenAdvertisementService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisCacheUtil<List<MgOpenAdvertisementEntity>> redisCacheUtil;

    /**
     * 获取开屏广告页
     */
    @Cacheable
    @IgnoreAuth
    @RequestMapping(value = "queryOpeningImages", produces = "application/json;charset=utf-8;")
    public R queryOpeningImages(@RequestParam Map<String, Object> params) throws Exception {
        String countryCode = "";
        String languageCode = "";
        String deviceType = "";
        String resolution = "";
        if (params.containsKey("countryCode")) {
            countryCode = (String) params.get("countryCode");
        } else {
            params.put("countryCode", "GB");
        }

        if (params.containsKey("languageCode")) {
            languageCode = (String) params.get("languageCode");
        } else {
            params.put("languageCode", "en");
        }
        if (params.containsKey("deviceType")) {
            deviceType = (String) params.get("deviceType");
        }

        try {
            List<MgOpenAdvertisementEntity> openingImage = null;
            String key = "queryOpeningImages_" + countryCode + "_" + languageCode + "_" + deviceType;
            if (redisCacheUtil.hashKey(key)) {
                openingImage = redisCacheUtil.getCacheList(key);
                if (params.containsKey("flush")) {  //刷新标志
                    redisCacheUtil.deleteObject(key);
                }
            } else {
                //查询数据库写入redis
                synchronized (this) {
                    openingImage = mgOpenAdvertisementService.queryOpeningImage(params);
                    redisCacheUtil.setCacheList(key, openingImage);
                }
            }
            if (openingImage == null) {
                return R.ok("result is null");
            } else {
                return R.ok("000000").put("data", openingImage);
                //return R.ok("000000").put("data", JsonUtils.getMapFromJson(JsonUtils.getJSONString(openingImage)));
            }
        } catch (Exception e) {
            return R.error();
        }
    }


}
